package com.howie.shirojwt.controller;

import com.howie.shirojwt.mapper.UserDaoMapper;
import com.howie.shirojwt.mapper.UserMapper;
import com.howie.shirojwt.model.*;
import com.howie.shirojwt.dto.UserInfo;
import com.howie.shirojwt.util.JWTUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * Created with IntelliJ IDEA
 *
 * @Description
 */
@RestController
public class LoginController {
    private final UserDaoMapper userDaoMapper;
    private final ResultMap resultMap;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    public LoginController(UserDaoMapper userDaoMapper, ResultMap resultMap) {
        this.userDaoMapper = userDaoMapper;
        this.resultMap = resultMap;
    }

    @PostMapping("/login")
    public ResultMap login(@RequestParam("username") String username,
                           @RequestParam("password") String password) {
        String realPassword = userDaoMapper.getPassword(username);
        if (realPassword == null) {
            return resultMap.fail().code(401).message("用户名错误");
        } else if (!realPassword.equals(password)) {
            return resultMap.fail().code(401).message("密码错误");
        } else {
            LoginUserInfo loginUserInfo=new LoginUserInfo();
            loginUserInfo.setJwtToken(JWTUtil.createToken(username));
            loginUserInfo.setUsername(username);

            return resultMap.success().code(200).message(loginUserInfo);
        }
    }


    @PostMapping("/login/vue")
    public ResultMap loginV2(@RequestParam("username") String username,
                           @RequestParam("password") String password) {
        String realPassword = userDaoMapper.getPassword(username);

        if (realPassword == null) {
            return resultMap.fail().code(401).message("用户名错误");
        } else if (!realPassword.equals(password)) {
            return resultMap.fail().code(401).message("密码错误");
        } else {
            UserExample example=new UserExample();
            UserExample.Criteria cri=example.createCriteria();
            cri.andUsernameEqualTo(username);
            List<User> users=userMapper.selectByExample(example);
            User user=users.get(0);

            String token=JWTUtil.createToken(username);
            LoginUserInfoVue loginUserInfo=new LoginUserInfoVue();

            UserInfo userInfo=new UserInfo();
            userInfo.setUsername(username);
            userInfo.setId(user.getId());
//            userInfo.setToken(token);


            loginUserInfo.setUserInfo(userInfo);
            loginUserInfo.setToken(token);
            loginUserInfo.setUsername(username);

            return resultMap.success().code(200).message(null).result(loginUserInfo);
        }
    }

    @RequestMapping(path = "/unauthorized/{message}")
    public ResultMap unauthorized(@PathVariable String message) throws UnsupportedEncodingException {
        return resultMap.success().code(401).message(message);
    }
}
